package com.zhss.designpattern.observer;

import java.util.Observable;
import java.util.Observer;

public class ObserverPatternDemo {

    public static void main(String[] args) {
        Subject subject = new Subject(0);

        Observer observer = new Subject.ConcreteObserver();
        subject.addObserver(observer);

        subject.setState(1);
        subject.setState(8);
    }

    public static class Subject extends Observable{
        private Integer state;

        public Subject(Integer state) {
            this.state = state;
        }

        public Integer getState() {
            return state;
        }

        public void setState(Integer state) {
            // 在这里状态就改变了
            this.state = state;
            // 通知关联的一些观察者，说我的状态变化了
            this.setChanged();
            this.notifyObservers(this);
        }

        public static class ConcreteObserver implements Observer {
            @Override
            public void update(Observable o, Object arg) {
                Subject subject = (Subject) o;
                System.out.println("目标对象的状态变化成：" + subject.getState());
            }
        }
    }
}
